package com;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/*
给定一个不含重复数字的数组 nums ，返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。

示例 1：

输入：nums = [1,2,3]
输出：[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
示例 2：

输入：nums = [0,1]
输出：[[0,1],[1,0]]
示例 3：

输入：nums = [1]
输出：[[1]]

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/permutations
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class Test {

    public static void main(String[] args) {

        List<int[]> testArray = new ArrayList<>();
        testArray.add(new int[]{1, 2, 3});
        testArray.add(new int[]{0, 1});
        testArray.add(new int[]{1});

        List<List<List<Integer>>> answer = new ArrayList<>();
        answer.add( twoDimensionalArray(new int[][]{{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}) );
        answer.add( twoDimensionalArray(new int[][]{{0, 1}, {1, 0}}) );
        answer.add( twoDimensionalArray(new int[][]{{1}}) );

        int total_case = testArray.size();
        int success_case = 0;
        com.solution.depthFirstSearch.Solution solution = new com.solution.depthFirstSearch.Solution();  // 深度优先
        for ( int i = 0 ; i < total_case ; i++ ) {
            List<List<Integer>> result = solution.permute(testArray.get(i));
            if ( sameCompare(answer.get(i), result) )
                success_case++;
        }

        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(2);
        df.setMinimumFractionDigits(2);
        String percentage = df.format((double) success_case / total_case * 100.00) + "%";
        System.out.println("测试案例通过率：" + percentage);

    }

    public static List<List<Integer>> twoDimensionalArray(int[][] arrays) {
        List<List<Integer>> result = new ArrayList<>();
        for ( int i = 0 ; i < arrays.length ; i++ )
            result.add(
                    Arrays.stream(arrays[i]).boxed().collect(Collectors.toList())
            );
        return result;
    }

    public static boolean sameCompare(List<List<Integer>> compA, List<List<Integer>> compB) {
        // 1.Unable to compare a set of empty objects.
        if ( compA == null || compB == null )
            return false;
        // 2.Can't compare when arrays are not the same size.
        int aSize = compA.size();
        int bSize = compB.size();
        if ( aSize != bSize )
            return false;
        // 3.Returns false if any collection contains duplicate objects.
        HashSet<List<Integer>> aSet = new HashSet<>(aSize);
        aSet.addAll(compA);
        int aSetSize = aSet.size();
        HashSet<List<Integer>> bSet = new HashSet<>(bSize);
        bSet.addAll(compB);
        int bSetSize = bSet.size();
        if ( aSize != aSetSize || bSize != bSetSize )
            return false;
        // 4.Compares if two arrays are exactly equal.
        boolean aContainsB = compA.containsAll(compB);
        boolean bContainsA = compB.containsAll(compA);
        if ( !aContainsB || !bContainsA )
            return false;
        return true;
    }

}
